import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { RedisModule } from './common/redis/redis.module';
import { JwtModule } from '@nestjs/jwt';
import config from '../config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { PmpModule } from './pmp/pmp.module';
import { LoggerMiddleware } from '@/middleware/logger.middleware';

function getModule() {
  switch (config.jwt) {
    case 'pmp':
      return PmpModule;
  }
  return PmpModule;
}

@Module({
  imports: [
    JwtModule.register({
      global: true,
      secret: config.jwt,
      signOptions: { expiresIn: '43200s' },
    }),
    TypeOrmModule.forRoot(config.db),
    RedisModule,
    getModule(),
  ],
  controllers: [],
  providers: [],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer): any {
    consumer.apply(LoggerMiddleware).forRoutes('*');
  }
}
